<?php
class ControllerExtensionModuleMlist extends Controller {
	private $error = array();

	public function index() {
		$this->load->language('extension/module/mlist');
		$this->load->model('localisation/language');
		$this->load->model('tool/image');
		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('extension/module');

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
			if (!isset($this->request->get['module_id'])) {
				$this->model_extension_module->addModule('mlist', $this->request->post);
			} else {
				$this->model_extension_module->editModule($this->request->get['module_id'], $this->request->post);
			}

			$this->session->data['success'] = $this->language->get('text_success');

			$this->response->redirect($this->url->link('extension/extension', 'token=' . $this->session->data['token'], 'SSL'));
		}

$data['language'] = current($this->model_localisation_language->getLanguages());
		$data['heading_title'] = $this->language->get('heading_title');

		$data['text_edit'] = $this->language->get('text_edit');
		$data['text_enabled'] = $this->language->get('text_enabled');
		$data['text_disabled'] = $this->language->get('text_disabled');

		$data['entry_title'] = $this->language->get('entry_title');
		$data['entry_link'] = $this->language->get('entry_link');
		$data['entry_status'] = $this->language->get('entry_status');
		$data['entry_name'] = $this->language->get('entry_name');
		$data['entry_image'] = $this->language->get('entry_image');
		$data['entry_price'] = $this->language->get('entry_price');
		$data['entry_des'] = $this->language->get('entry_des');
		$data['entry_list1'] = $this->language->get('entry_list1');
		$data['entry_list1_bgcolor'] = $this->language->get('entry_list1_bgcolor');
		$data['entry_list1_bg'] = $this->language->get('entry_list1_bg');
		$data['entry_list2'] = $this->language->get('entry_list2');
		$data['entry_list3'] = $this->language->get('entry_list3');
		$data['entry_list4'] = $this->language->get('entry_list4');

		$data['button_save'] = $this->language->get('button_save');
		$data['button_cancel'] = $this->language->get('button_cancel');
		$data['button_add'] = $this->language->get('button_add');
		$data['button_remove'] = $this->language->get('button_remove');


		if (isset($this->error['warning'])) {
			$data['error_warning'] = $this->error['warning'];
		} else {
			$data['error_warning'] = '';
		}

		if (isset($this->error['name'])) {
			$data['error_name'] = $this->error['name'];
		} else {
			$data['error_name'] = '';
		}

		$data['breadcrumbs'] = array();

		$data['breadcrumbs'][] = array(
			'text' => $this->language->get('text_home'),
			'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], 'SSL')
		);

		$data['breadcrumbs'][] = array(
			'text' => $this->language->get('text_module'),
			'href' => $this->url->link('extension/extension', 'token=' . $this->session->data['token'], 'SSL')
		);

		if (!isset($this->request->get['module_id'])) {
			$data['breadcrumbs'][] = array(
				'text' => $this->language->get('heading_title'),
				'href' => $this->url->link('extension/module/mlist', 'token=' . $this->session->data['token'], 'SSL')
			);
			$data['action'] = $this->url->link('extension/module/mlist', 'token=' . $this->session->data['token'], 'SSL');

		} else {
			$data['breadcrumbs'][] = array(
				'text' => $this->language->get('heading_title'),
				'href' => $this->url->link('extension/module/mlist', 'token=' . $this->session->data['token'] . '&module_id=' . $this->request->get['module_id'], 'SSL')
			);
			$data['action'] = $this->url->link('extension/module/mlist', 'token=' . $this->session->data['token'] . '&module_id=' . $this->request->get['module_id'], 'SSL');
		}

		$data['cancel'] = $this->url->link('extension/extension', 'token=' . $this->session->data['token'], 'SSL');

		if (isset($this->request->get['module_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
			$module_info = $this->model_extension_module->getModule($this->request->get['module_id']);
		}

		if (isset($this->request->post['status'])) {
			$data['status'] = $this->request->post['status'];
		} elseif (isset($module_info['status'])) {
			$data['status'] = $module_info['status'];
		} else {
			$data['status'] = '';
		}
		if (isset($this->request->post['name'])) {
			$data['name'] = $this->request->post['name'];
		} elseif (isset($module_info['name'])) {
			$data['name'] = $module_info['name'];
		} else {
			$data['name'] = '';
		}
		if (isset($this->request->post['list1'])) {
			$data['list1'] = $this->request->post['list1'];
		} elseif (isset($module_info['list1'])) {
			$data['list1'] = $module_info['list1'];
		} else {
			$data['list1'] = array();
		}	
		if (isset($this->request->post['list3'])) {
			$data['list3'] = $this->request->post['list3'];
		} elseif (isset($module_info['list3'])) {
			$data['list3'] = $module_info['list3'];
		} else {
			$data['list3'] = array();
		}
			if (isset($this->request->post['list1_title'])) {
			$data['list1_title'] = $this->request->post['list1_title'];
		} elseif (isset($module_info['list1_title'])) {
			$data['list1_title'] = $module_info['list1_title'];
		} else {
			$data['list1_title'] = '';
		}
		if (isset($this->request->post['list1_des'])) {
			$data['list1_des'] = $this->request->post['list1_des'];
		} elseif (isset($module_info['list1_des'])) {
			$data['list1_des'] = $module_info['list1_des'];
		} else {
			$data['list1_des'] = '';
		}
		if (isset($this->request->post['list1_link'])) {
			$data['list1_link'] = $this->request->post['list1_link'];
		} elseif (isset($module_info['list1_link'])) {
			$data['list1_link'] = $module_info['list1_link'];
		} else {
			$data['list1_link'] = '';
		}
		if (isset($this->request->post['list1_bgcolor'])) {
			$data['list1_bgcolor'] = $this->request->post['list1_bgcolor'];
		} elseif (isset($module_info['list1_bgcolor'])) {
			$data['list1_bgcolor'] = $module_info['list1_bgcolor'];
		} else {
			$data['list1_bgcolor'] = '';
		}
		if (!empty($list1_bg)&&is_file(DIR_IMAGE . $list1_bg)) {
			$data['list1_bg'] = $this->request->post['list1_bg'];
		} else {
			$data['list1_bg'] = 'no_image.png';
		}
		$data['list1_bg_thumb']=$this->model_tool_image->resize($data['list1_bg'], 100, 100);
		if (isset($this->request->post['list3_title'])) {
			$data['list3_title'] = $this->request->post['list3_title'];
		} elseif (isset($module_info['list3_title'])) {
			$data['list3_title'] = $module_info['list3_title'];
		} else {
			$data['list3_title'] = '';
		}
		if (isset($this->request->post['list3_link'])) {
			$data['list3_link'] = $this->request->post['list3_link'];
		} elseif (isset($module_info['list3_link'])) {
			$data['list3_link'] = $module_info['list3_link'];
		} else {
			$data['list3_link'] = '';
		}
		if (isset($this->request->post['list4_title'])) {
			$data['list4_title'] = $this->request->post['list4_title'];
		} elseif (isset($module_info['list4_title'])) {
			$data['list4_title'] = $module_info['list4_title'];
		} else {
			$data['list4_title'] = '';
		}
		if (isset($this->request->post['list4_des'])) {
			$data['list4_des'] = $this->request->post['list4_des'];
		} elseif (isset($module_info['list4_des'])) {
			$data['list4_des'] = $module_info['list4_des'];
		} else {
			$data['list4_des'] = '';
		}

		if (isset($this->request->post['list2'])) {
			$tmplist2  = $this->request->post['list2'];
		} elseif (isset($module_info['list2'])) {
			$tmplist2  = $module_info['list2'];
		} else {
			$tmplist2 = array();
		}

		$data['list2']=array();
		foreach ($tmplist2 as $setting) {
			if (!empty($setting['image'])&&is_file(DIR_IMAGE . $setting['image'])) {
				$thumb = $setting['image'];
			} else {
				$thumb = 'no_image.png';
			}
			$data['list2'][] = array(
				'title' => $setting['title'],
				'link'  => $setting['link'],
				'image'  => $setting['image'],
				'price'  => $setting['price'],
				'thumb' => $this->model_tool_image->resize($thumb, 100, 100),
			);
		}
		$data['placeholder'] = $this->model_tool_image->resize('no_image.png', 100, 100);
		$data['header'] = $this->load->controller('common/header');
		$data['column_left'] = $this->load->controller('common/column_left');
		$data['footer'] = $this->load->controller('common/footer');

		$this->response->setOutput($this->load->view('extension/module/mlist.tpl', $data));
	}

	protected function validate() {
		if (!$this->user->hasPermission('modify', 'extension/module/mlist')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}
		if ((utf8_strlen($this->request->post['name']) < 2) || (utf8_strlen($this->request->post['name']) > 64)) {
			$this->error['name'] = $this->language->get('error_name');
		}
		return !$this->error;
	}
}